package com.cooksys.film_rental.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.cooksys.film_rental.dao.StoreDao;
import com.cooksys.film_rental.model.Address;
import com.cooksys.film_rental.model.Customer;
import com.cooksys.film_rental.model.Inventory;
import com.cooksys.film_rental.model.Staff;
import com.cooksys.film_rental.model.Store;

@Repository("StoreDao")
@Transactional
public class StoreDaoImpl implements StoreDao {

	@Autowired(required = true)
	private SessionFactory sessionFactory;

	/**
	 * 
	 * @return the current session from the internal {@link SessionFactory}
	 */
	public Session currentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public Store getStoreByStoreId(Byte storeId) {
		Session session = currentSession();

		// returns a record from the STORE table as an Store object
		// based on the given storeId, the table's primary key
		String hql = "from Store s where s.storeId = :storeId";

		Query query = session.createQuery(hql);
		query.setByte("storeId", storeId);

		return (Store) query.list().get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Store> searchStoresByStaff(Staff staff) {
		Session session = currentSession();

		String hql = "from Store s where s.staff = :staff";
		Query query = session.createQuery(hql);
		query.setEntity("staff", staff);

		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Store> searchStoresByAddress(Address address) {
		Session session = currentSession();

		String hql = "from Store s where s.address = :address";
		Query query = session.createQuery(hql);
		query.setEntity("address", address);

		return query.list();
	}

	@Override
	public Store getStoreByInventory(Inventory inventory) {
		Session session = currentSession();

		String hql = "select i.store from Inventory i where i = :inventory";
		Query query = session.createQuery(hql);
		query.setEntity("inventory", inventory);

		return (Store) query.list().get(0);
	}

	@Override
	public Store getStoreByCustomer(Customer customer) {
		Session session = currentSession();

		String hql = "select c.store from Customer c where c = :customer";
		Query query = session.createQuery(hql);
		query.setEntity("customer", customer);

		return (Store) query.list().get(0);
	}

	@Override
	public Store getStoreByStaff(Staff staff) {
		Session session = currentSession();

		String hql = "select s.store from Staff s where s = :staff";
		Query query = session.createQuery(hql);
		query.setEntity("staff", staff);

		return (Store) query.list().get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Store> getAllStores() {
		Session session = currentSession();

		String hql = "from Store";
		Query query = session.createQuery(hql);

		return query.list();
	}

}
